package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.unionChest.config.UnionChestBoxConfig;
import com.yanqu.road.entity.activity.unionChest.config.UnionChestConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.gameplayer.module.activity.goddess.GoddessConfig;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.TempGoodsBagMgr;
import com.yanqu.road.server.manger.activity.goddess.GoddessMgr;
import com.yanqu.road.server.manger.activity.unionchest.UnionChestMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class UnionChestRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        UnionChestConfig config = UnionChestMgr.getConfig();
        if (config != null && config.getActivityInfo() != null) {
            List<String> paramList = new ArrayList<>();
            paramList.add(String.valueOf(config.getActivityInfo().getActivityId()));
            paramList.add(config.getActivityInfo().getActivityName());
            long recycleTime = config.getActivityInfo().getEndShowTime() * 1000L + 600 * DateHelper.SECOND_MILLIONS;
            for (UnionChestBoxConfig boxConfig : config.getBoxMap().values()) {
                ActivityItemRecycleMgr.addRecycleItemData(boxConfig.getBoxId(), activityType, 1, recycleTime, paramList);
            }
        }
    }

    @Override
    protected void initActivityType() {
        this.activityType = eActivityType.UnionChest.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int goodsId = data.getItemId();
        UserBagItem userBagItem = getUserBagItem(userId, goodsId);
        if (userBagItem == null || userBagItem.getNum() <= 0) {
            return;
        }
        Property property = new Property();
        for (long i = 0; i < userBagItem.getNum(); i++) {
            OpenGoodsBagResult randomGoods = GoodsBagMgr.getRandomGoods(goodsId);
            if (null == randomGoods) {
                UnionChestMgr.getLogger().error("{}活动{},玩家{}回收道具{}时错误!",
                        data.getParamList().get(0), data.getParamList().get(1), userBagItem.getUserId(), userBagItem.getGoodsId());
                return;
            }
            property.addProperty(randomGoods.getReward());

        }
        String language = UserMgr.getUserInfo(userId).getLanguage();
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(userBagItem.getGoodsId());
        long removeNum = userBagItem.getNum();
        if (removeItem(userBagItem)) {
            //  发邮件
            String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
            String activityName = ServerLanguageMgr.getContent(data.getParamList().get(1), language);
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_TITLE, language), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_CONTENT,
                    language), removeName, removeNum);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property), mailContent, mailTitle);
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
